/**
 * Desenvolvido pela Fábrica de Software do CESUPA.
 * Todos os direitos reservados.
 */
package br.cesupa.fabsoft.nomedoprojeto.modules.helpmodule;

import br.cesupa.fabsoft.nomedoprojeto.modules.helpmodule.entities.HelpSet;
import br.cesupa.fabsoft.nomedoprojeto.modules.helpmodule.entities.HelpTopic;
import br.cesupa.fabsoft.nomedoprojeto.modules.helpmodule.services.HelpService;
import java.io.FileWriter;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;

/**
 *
 * @author Breno Leite
 */
public class HelpSummaryWriter {

    /**
     * Creates a XML representation of a given help set.
     *
     * @param helpSet the target help set
     */
    public void generateHelpSetSummaryFile(HelpSet helpSet) {
        try {
            Document document = DocumentHelper.createDocument();

            Element root = DocumentHelper.createElement("helpsets-summary");
            iterateOverHelpSet(root, helpSet);
            document.add(root);

            XMLWriter writer = new XMLWriter(
                    new FileWriter(HelpService.getHelpSetsSummaryFile()));
            writer.write(document);
            writer.close();
        } catch (Exception exception) {
        }
    }

    private void iterateOverHelpSet(Element parentElement, HelpSet helpSet) {
        for (HelpTopic helpTopic : helpSet.getHelpTopics()) {
            parentElement.add(createHelpTopicElement(helpTopic));
        }

        for (HelpSet set : helpSet.getHelpSets()) {
            Element helpSetElement = createHelpSetElement(set);
            iterateOverHelpSet(helpSetElement, set);
            parentElement.add(helpSetElement);
        }
    }

    /**
     * Creates a xml element from a {@link HelpTopic}.
     *
     * @param helpTopic the target help topic
     * @return a xml element
     */
    private Element createHelpTopicElement(HelpTopic helpTopic) {
        Element helpTopicElement = DocumentHelper.createElement("help-topic");

        Element pathElement = DocumentHelper.createElement("path");
        pathElement.setText(helpTopic.getPath());
        helpTopicElement.add(pathElement);

        Element titleElement = DocumentHelper.createElement("title");
        titleElement.setText(helpTopic.getTitle());
        helpTopicElement.add(titleElement);

        Element keywordsElement = DocumentHelper.createElement("keywords");
        keywordsElement.setText(helpTopic.getKeywords());
        helpTopicElement.add(keywordsElement);

        return helpTopicElement;
    }

    /**
     * Creates a xml element from a {@link HelpSet}.
     *
     * @param helpSet the target help set
     * @return a xml element
     */
    private Element createHelpSetElement(HelpSet helpSet) {
        Element helpSetElement = DocumentHelper.createElement("help-set");

        helpSetElement.addAttribute("title", helpSet.getTitle());
        helpSetElement.addAttribute("description", helpSet.getDescription());

        return helpSetElement;
    }
}
